package cn.ppwx.crm;

import java.util.Date;
import java.util.List;

import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.ppwx.crm.common.log.CrmLogger;
import cn.ppwx.crm.modules.sys.entity.Purchase;
import cn.ppwx.crm.modules.sys.entity.User;
import cn.ppwx.crm.modules.sys.listener.SessionCache;
import cn.ppwx.crm.modules.sys.service.PurchaseService;
import cn.ppwx.crm.modules.sys.service.UserService;
import cn.ppwx.crm.modules.sys.service.WorkFlowService;

/**
 * Created by peter.pi on 16/11/25.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-context.xml" ,"classpath:spring-context-activiti.xml" })  
public class testPurchase {


	@Autowired
	private  WorkFlowService workFlowService;
	
	
	@Autowired
	private PurchaseService purchaseService;
	
	
	@Autowired
	private UserService userService;
	

	
	/**
	 * 部署一个流程定义，实际开发中，可以做一个按zip自定义的部署 
	 */
	@Test
	public void testDep(){
		
		workFlowService.deploymentPurchaseProcessDefinition();
		
	}

	
	
	/**
	 *采购小妹先启动一个流程
	 */
	@Test
	public  void testApplyLeave(){
		
		
		User  user=new User();
		user.setUserId(1003L);
		user.setUserName("采购员小陈");
		user.setManagerId(1002L);
		SessionCache.loginUser.put("user", user);
		
		
		Purchase purchase = new Purchase();
		
		purchase.setPurchaseUserId(4L);
		purchase.setArrivTime(new Date());
		purchase.setArrivVender("华北仓");
		purchase.setProductName("苹果电脑");
		purchase.setPrice("5000");
		purchase.setPurQty(10);
		purchase.setSupplierName("珠海苹果数码有限公司");
		
		purchaseService.createPurchase(purchase);
		
	}
	
	/**
	 * 采购小妹查询当前的流程任务
	 */
	@Test
	public  void testQueryToDoList(){
		
		User  user=new User();
		user.setUserId(1003L);
		user.setUserName("采购员小陈");
		user.setManagerId(1002L);
		SessionCache.loginUser.put("user", user);
		
		List<Task> tasks = userService.queryTodoTask(user);
		
		if(tasks!=null&&tasks.size()>0){
			
			CrmLogger.log.info(tasks);
			
		}
	}
	
	
	/**
	 * 采购员小妹提交流程
	 */
	@Test
	public void testCheckApply_1(){
		
	
		User  user=new User();
		user.setUserId(1003L);
		user.setUserName("采购员小陈");
		user.setManagerId(1002L);
		SessionCache.loginUser.put("user", user);
		
		Task task= userService.queryTodoTask(user).get(0);
		
		CrmLogger.log.info(task.getId()+" "+task.getAssignee());
		
		workFlowService.completeTask(task.getId(),"货没了，得补");
		
		
	}
	
	

	/**
	 * 采购经理逐级审批或驳回
	 */
	@Test
	public void testCheckApply(){
		
		String approveResult="true";
		
		User  user=new User();
		user.setUserId(1002L);
		user.setUserName("采购经理老吴");
		user.setManagerId(1001L);
		SessionCache.loginUser.put("user", user);
		Task task= userService.queryTodoTask(user).get(0);
		
		CrmLogger.log.info(task.getId()+" "+task.getAssignee());
		purchaseService.manageApprove(task.getId(), user, approveResult,"同意");
		
		
	}



	/**
	 * 采购 总监逐级审批或驳回
	 */
	@Test
	public void testCheckApply_2(){
		
		String approveResult="true";
		
		User  user=new User();
		user.setUserId(1001L);
		user.setUserName("采购总监老田");
		user.setManagerId(0L);
		SessionCache.loginUser.put("user", user);
		Task task= userService.queryTodoTask(user).get(0);
		
		CrmLogger.log.info(task.getId()+" "+task.getAssignee());
		purchaseService.manageApprove(task.getId(), user, approveResult,"同意");
		
		
	}




	
	
	/**
	 * 财务经理审批通过
	 */
	@Test
	public void testCheckApply_4(){
		User  user=new User();
		user.setUserId(2001L);
		user.setManagerId(0L);
		user.setUserName("CFO");
		SessionCache.loginUser.put("user", user);
		Task task= userService.queryTodoTask(user).get(0);
		
		CrmLogger.log.info(task.getId()+" "+task.getAssignee());
		
		String approveResult="true";
		purchaseService.manageApprove(task.getId(), user, approveResult,"同意进货");
	}
	
	
	
	
	/**
	 * 发票登记员登记
	 */
	@Test
	public void testCheckApply_5(){
		User  user=new User();
		user.setUserId(2005L);
		user.setManagerId(2001L);
		user.setUserName("发票员小何");
		SessionCache.loginUser.put("user", user);
		Task task= userService.queryTodoTask(user).get(0);
		
		CrmLogger.log.info(task.getId()+" "+task.getAssignee());
		workFlowService.completeTask(task.getId(),"进项发票已登记");
	}

	
	
	
	/**
	 * 仓管员登记
	 */
	@Test
	public void testCheckApply_6(){
		User  user=new User();
		user.setUserId(3001L);
		user.setManagerId(0L);
		user.setUserName("仓管员小黄");
		SessionCache.loginUser.put("user", user);
		Task task= userService.queryTodoTask(user).get(0);
		
		CrmLogger.log.info(task.getId()+" "+task.getAssignee());
		workFlowService.completeTask(task.getId(),"已入仓库");
	}
	
	
	
	
	/**
	 * 供应商结算勾兑
	 */
	@Test
	public void testCheckApply_7(){
		User  user=new User();
		user.setUserId(2004L);
		user.setManagerId(2001L);
		user.setUserName("结算小李");
		SessionCache.loginUser.put("user", user);
		Task task= userService.queryTodoTask(user).get(0);
		
		CrmLogger.log.info(task.getId()+" "+task.getAssignee());
		workFlowService.completeTask(task.getId(),"已勾兑，同意");
	}
	
	
	
	/**
	 * 出纳出款
	 */
	@Test
	public void testCheckApply_8(){
		User  user=new User();
		user.setUserId(2003L);
		user.setManagerId(2001L);
		user.setUserName("出纳小张");
		SessionCache.loginUser.put("user", user);
		Task task= userService.queryTodoTask(user).get(0);
		
		CrmLogger.log.info(task.getId()+" "+task.getAssignee());
		workFlowService.completeTask(task.getId(),"已发款");
	}

}
